package operations;

import java.awt.FileDialog;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import controller.EditorController;




/**
 * The Class FileOperations.
 * This class performs all the file I/O
 */
public class FileOperations
{
	public FileOperations() throws IOException, NullPointerException
	{
		
		// TODO Auto-generated constructor stub
	}

	private String stream=" ";
	private int fileLength;
	
	/**
	 * Open file.
	 * This file opens an existing file. Get the name of the file which the user
	 * would like to open into the variable fileName.
	 * @param fileName the file name
	 * @return the string
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	 public String Openfile(String fileName) throws IOException{
	       
	        StringBuilder textFile = new StringBuilder();
	        String NL = System.getProperty("line.separator");
	        Scanner scanner = new Scanner(new FileInputStream(fileName));
	        try    {
	        while (scanner.hasNextLine()){
	            textFile.append(scanner.nextLine() + NL);
	        }   
	        }
	        finally{
	            scanner.close();
	        }
	        return textFile.toString();
	    }
	
	/**
	 * Save as.
	 * This function is used to save a new file.
	 * @param stream the stream
	 * @param fileName the file name
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public void saveAs(String stream, String fileName) throws IOException
	{
		fileLength=stream.length();
		byte buffer[]=stream.getBytes();
		
		File file=new File(fileName);
		FileOutputStream fileStream=new FileOutputStream(file);
		for(int counter=0;counter<fileLength;counter++)
		{
			fileStream.write(buffer[counter]);
		}
	    	fileStream.close();
		    EditorController.unSavedFile=false;	
	
	
	}
	
	/**
	 * Save.
	 * This function is used to save an already existing file 
	 * @param stream the stream
	 * @param fileName the file name
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public void Save(String stream, String fileName) throws IOException
	{

		  	
			fileLength=stream.length();
			byte buffer[]=stream.getBytes();
			File file=new File(fileName);
			FileOutputStream fileStream=new FileOutputStream(file);
			for(int counter=0;counter<fileLength;counter++)
			{
				fileStream.write(buffer[counter]);
			}
		    	fileStream.close();
			    EditorController.unSavedFile=false;	
  	
	
		
	}
	
	/**
	 * Newfile.
	 * This function is used to create a new file.
	 */
	public void Newfile()
	{	
		EditorController note;
		try {
			note = new EditorController();

			note.setVisible(true);
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
	    EditorController.unSavedFile=false;	
	
		}


	


}
